package com.better.consist;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：枚举工厂，主要提供根据名称获取对应的枚举对象，和枚举值封装
 *
 */
@SuppressWarnings("rawtypes")
public class EnumFactory {
	/**
	 * 所有枚举单例的Map,存放枚举字符串名称和对应的Class
	 */

	private static Map<String, Class> SINGLE_ENUM_MAP = new HashMap<String, Class>();

	/**
	 * 获取没枚举所有值类型的封装 <br/>
	 * Map[Integer(分组TeamId), List[Map[code编码, name中文名称]]]
	 * 
	 * @param clazz
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static Map<Integer, List<Map<String, String>>> getEnumValuesMap(Class clazz) {
		Map<Integer, List<Map<String, String>>> enumMap = new HashMap<Integer, List<Map<String, String>>>();
		try {
			IEnumBase[] es = (IEnumBase[]) clazz.getDeclaredMethod("values").invoke(null);
			List list = null;
			for (IEnumBase e : es) {
				if (enumMap.get(e.getTeamId()) == null) {
					list = new ArrayList();
					enumMap.put(e.getTeamId(), list);
				}
				list = enumMap.get(e.getTeamId());
				Map<String, String> map = new HashMap<String, String>();
				map.put("code", e.getCode().toString());
				map.put("name", e.getName());
				list.add(map);
			}
		} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException
				| SecurityException e1) {
			e1.printStackTrace();
		}
		return enumMap;
	}

	/** 静态初始化块,所以的枚举都在此进行注册 */
	static {
		SINGLE_ENUM_MAP.put("globalTypeEnum", GlobalTypeEnum.class);
	}

	/**
	 * 获取枚举的分组值，根据枚举名称和分组Id
	 * 
	 * @param enumName
	 * @param teamId
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public static List<Map<String, String>> getEnumValuesByEnumNameAndTeamId(String enumName, Integer teamId)
			throws Exception {
		Class clazz = SINGLE_ENUM_MAP.get(enumName);
		Map<Integer, List<Map<String, String>>> map = (Map<Integer, List<Map<String, String>>>) clazz
				.getField("EnumMap").get(clazz);
		return (List<Map<String, String>>) map.get(teamId);
	}

	/**
	 * 测试测试
	 * 
	 * @param a
	 */
	public static void main(String[] str) {
		try {
			List<Map<String, String>> list1 = EnumFactory.getEnumValuesByEnumNameAndTeamId("compInfoEnum", 1);
			List<Map<String, String>> list2 = EnumFactory.getEnumValuesByEnumNameAndTeamId("dictionaryEnum", 1);
			List<Map<String, String>> list3 = EnumFactory.getEnumValuesByEnumNameAndTeamId("globalTypeEnum", 1);
			List<Map<String, String>> list4 = EnumFactory.getEnumValuesByEnumNameAndTeamId("merchantEnum", 1);
			List<Map<String, String>> list5 = EnumFactory.getEnumValuesByEnumNameAndTeamId("orderStatusEnum", 1);
			List<Map<String, String>> list6 = EnumFactory.getEnumValuesByEnumNameAndTeamId("storageTypeEnum", 3);
			List<Map<String, String>> list7 = EnumFactory.getEnumValuesByEnumNameAndTeamId("sysUserEnum", 1);
			List<Map<String, String>> list8 = EnumFactory.getEnumValuesByEnumNameAndTeamId("transportPlanEnum", 1);
			System.out.println(list1);
			System.out.println(list2);
			System.out.println(list3);
			System.out.println(list4);
			System.out.println(list5);
			System.out.println(list6);
			System.out.println(list8);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
